/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Clases;

import java.io.File;


/**
 *
 * @author Victor
 */
public class Mantenimiento {
    
    MFecha Fecha = new MFecha();
    String PathExt = new File("Ext").getAbsolutePath();
    Clases.Conexion Conex = new Clases.Conexion();
    private static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(Mantenimiento.class);

    public Mantenimiento() {        
        
    }
    
    public boolean Backup(String DestinoPath){        
        String executeCmd = PathExt+File.separator+"mysqldump.exe -u " + Conex.getUser() + " -p" + Conex.getPass() + " --add-drop-database -B " 
                            + Conex.getBd() + " -r " + DestinoPath;
        Process runtimeProcess;
        try {
 
            runtimeProcess = Runtime.getRuntime().exec(executeCmd);
            int processComplete = runtimeProcess.waitFor(); 
            if (processComplete == 0) {        
                log.info("Backup generado con exito..");
                return true;
            } 
        } catch (Exception ex) {            
            log.error(ex.getMessage());
        } 
        return false;   
    }
    
    public boolean Restore(String FromPath){
//        String[] executeCmd = new String[]{PathExt+File.separator+"mysql ", "-u " + Conex.getUser(), "-p " + 
//                Conex.getPass(), Conex.getBd()," ", "<"+FromPath};
        String executeCmd = PathExt+File.separator+"mysql.exe -u " + Conex.getUser() + " -p " + Conex.getPass() + " " 
                            + Conex.getBd() + " < " + FromPath;
        Process runtimeProcess;
        try {
 
            runtimeProcess = Runtime.getRuntime().exec(executeCmd);
            int processComplete = runtimeProcess.waitFor(); 
            if (processComplete == 0) {
                System.out.println("Backup restored successfully");
                return true;
            } else {
                System.out.println("Could not restore the backup");
            }
        } catch (Exception ex) {            
            log.error(ex.getMessage());
        }
 
        return false;
    
    }
    
}
